/* GUI CONVERTED from reupdgac.p (converter v1.76) Tue Apr 23 11:44:02 2002 */
/* reupdgac.p - REPETITIVE                                                    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.21 $                                                         */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 7.3      LAST MODIFIED: 11/08/94   BY: WUG *GO42*                */
/* REVISION: 7.3      LAST MODIFIED: 09/27/95   BY: STR *G0Y1*                */
/* REVISION: 7.3      LAST MODIFIED: 11/16/95   BY: JYM *G1DL*                */
/* REVISION: 7.3      LAST MODIFIED: 08/06/97   BY: *G2P0* Manmohan Pardesi   */
/* REVISION: 8.5      LAST MODIFIED: 02/13/98   BY: *J2F9* Santhosh Nair      */
/* REVISION: 9.0      LAST MODIFIED: 04/16/99   BY: *J2DG* Reetu Kapoor       */
/* REVISION: 9.1      LAST MODIFIED: 11/17/99   BY: *N04H* Vivek Gogte        */
/* REVISION: 9.1      LAST MODIFIED: 09/05/00   BY: *N0RF* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 11/10/00   BY: *M0W5* Mark Christian     */
/* REVISION: 9.1      LAST MODIFIED: 12/01/00   BY: *L0P5* Sathish Kumar      */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision: 1.20     BY: Katie Hilbert         DATE: 02/25/02  ECO: *N194*  */
/* $Revision: 1.21 $    BY: Robin McCarthy     DATE: 04/12/02  ECO: *P000*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* UPDATE G/L AVERAGE COST                                                    */
/* THE FOLLOWING IS THE ACCOUNT OF DESIGNATIONS AS USED IN ADVANCED REPETETIVE*/
/* COSTING. 'tl' REFERS TO THE CURRENT OPERATION and 'll' REFERS TO PREVIOUS  */
/* OPERATIONS. (THESE DESIGNATIONS DO NOT CORRELATE TO this-level AND lower-  */
/* level ITEM COSTS.)                                                         */
/* wr_xxx_act     - The actual labor, burden, and subcontract costs reported  */
/*                  for the quantity processed at this operation. The actual  */
/*                  material cost however is stored in the wr_mtl_ll_act      */
/*                  (G1GD) of the qad_wkfl.                                   */
/* wr_xxx_ll_act  - The accumulated labor, burden, and subcontract costs from */
/*                  the previous operations for the quantity processed at this*/
/*                  operation. The wr_mtl_ll_act however contains the         */
/*                  accumulated material costs for the quantity completed in  */
/*                  this operation.                                           */
/* wr_xxx_totx    - The total material, labor, burden, and subcontract cost   */
/*                  removed from WIP at this operation for the quantity       */
/*                  processed in the next operation                           */
/* wr_xxx_ll_totx - The accumulated total material, labor, burden, and        */
/*                  and subcontract cost removed from WIP from the previous   */
/*                  operations for the quantity completed in the next         */
/*                  operation. These are stored in the qad_wkfl.              */
/* unit_xxx_tl    - The local variables that contains the per unit material,  */
/*                  labor, burden, subcontract and overhead cost at the last  */
/*                  operation. These are used to update the items G/L Average */
/*                  cost and the Current Average or Last cost.                */
/* unit_xxx_ll    - The local variables that contains the per unit material,  */
/*                  labor, burden, subcontract and overhead cost at the last  */
/*                  operation from previous operations. These are used to     */
/*                  update the items G/L Average cost and the Current Average */
/*                  or Last cost.                                             */

{mfdeclre.i}

define input parameter wolot      as character     no-undo.
define input parameter qty_chg    as decimal       no-undo.
define output parameter unit_cost like sct_cst_tot no-undo.

define variable glx_mthd like cs_method no-undo.
define variable glx_set  like cs_set    no-undo.
define variable cur_mthd like cs_method no-undo.
define variable cur_set  like cs_set    no-undo.

define variable wip_qty            as decimal no-undo.
define variable lbr_ll_wipval_xfer as decimal no-undo.
define variable bdn_ll_wipval_xfer as decimal no-undo.
define variable sub_ll_wipval_xfer as decimal no-undo.
define variable mtl_ll_wipval_xfer as decimal no-undo.
define variable lbr_tl_wipval_xfer as decimal no-undo.
define variable bdn_tl_wipval_xfer as decimal no-undo.
define variable sub_tl_wipval_xfer as decimal no-undo.
define variable mtl_tl_wipval_xfer as decimal no-undo.
define variable unit_lbr_ll        as decimal no-undo.
define variable unit_bdn_ll        as decimal no-undo.
define variable unit_sub_ll        as decimal no-undo.
define variable unit_mtl_ll        as decimal no-undo.
define variable unit_ovh_ll        as decimal no-undo.
define variable unit_lbr_tl        as decimal no-undo.
define variable unit_bdn_tl        as decimal no-undo.
define variable unit_sub_tl        as decimal no-undo.
define variable unit_mtl_tl        as decimal no-undo.
define variable unit_ovh_tl        as decimal no-undo.
define variable newlbr_ll          as decimal no-undo.
define variable newbdn_ll          as decimal no-undo.
define variable newsub_ll          as decimal no-undo.
define variable newmtl_ll          as decimal no-undo.
define variable newovh_ll          as decimal no-undo.
define variable newlbr_tl          as decimal no-undo.
define variable newbdn_tl          as decimal no-undo.
define variable newsub_tl          as decimal no-undo.
define variable newmtl_tl          as decimal no-undo.
define variable newovh_tl          as decimal no-undo.
define variable newcst             as decimal no-undo.
define variable oldcst       like sct_cst_tot no-undo.
define variable reavg_yn     like mfc_logical no-undo.
define variable msgref       like tr_msg      no-undo.
define variable gl_amt       like glt_amt     no-undo.
define variable qty_oh             as decimal no-undo.
define variable new_qty_oh         as decimal no-undo.
define variable curflag      like mfc_logical no-undo.
define variable glxflag      like mfc_logical no-undo.
define variable use_last     like mfc_logical no-undo.
define variable l_ord_nbr    as   character   no-undo.
define variable rndmthd      like gl_rnd_mthd no-undo.

assign
   curflag = no
   glxflag = no
   use_last = no
   unit_cost = 0.
if qty_chg = 0 then leave.

for first gl_ctrl fields(gl_rnd_mthd) no-lock:
end.
if available gl_ctrl then rndmthd = gl_rnd_mthd.
for first icc_ctrl fields(icc_cogs icc_gl_sum icc_gl_tran icc_mirror)
no-lock:
end.
for first wo_mstr
   fields (wo_lot wo_part wo_site)
   where wo_lot = wolot no-lock:
end. /* FOR FIRST WO_MSTR */

for first in_mstr
   fields (in_part in_qty_nonet in_qty_oh in_site
           in_gl_cost_site in_gl_set)
   where in_site = wo_site
   and in_part = wo_part no-lock:
end. /* FOR FIRST IN_MSTR */
if available in_mstr then
   assign
      qty_oh     = in_qty_oh + in_qty_nonet
      new_qty_oh = qty_oh + qty_chg.

/* DETERMINE COSTING METHOD */
{gprun.i ""csavg01.p"" "(input wo_part, input wo_site,
     output glx_set, output glx_mthd, output cur_set, output cur_mthd)" }
/*GUI*/ if global-beam-me-up then undo, leave.


/* GET G/L COST SET */
{gpsct01.i &set=glx_set &part=wo_part &site=wo_site}
find last wr_route where wr_lot = wo_lot exclusive-lock.

wip_qty = wr_qty_outque + wr_qty_rejque.
if qty_chg > 0 then do:
   if wip_qty <> 0 then do:
      assign
         unit_lbr_ll = (wr_lbr_ll_act - wr_lbr_ll_totx) / wip_qty
         unit_bdn_ll = (wr_bdn_ll_act - wr_bdn_ll_totx) / wip_qty
         unit_sub_ll = (wr_sub_ll_act - wr_sub_ll_totx) / wip_qty
         unit_mtl_ll = (wr_mtl_ll_act - wr_mtl_ll_totx) / wip_qty

         unit_lbr_tl = (wr_lbr_act - wr_lbr_totx) / wip_qty
         unit_bdn_tl = (wr_bdn_act - wr_bdn_totx) / wip_qty
         unit_sub_tl = (wr_sub_act - wr_sub_totx) / wip_qty
         unit_mtl_tl = (wr_mtl_act - wr_mtl_totx) / wip_qty

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.

      /* ADDED THE FOLLOWING SECTION TO ENSURE THAT THE _wipval_xfer
         FIELDS WON'T CAUSE THEIR RESPECTIVE _totx FIELDS TO EXCEED
         THEIR RESPECTIVE _act FIELDS.  THEN THE UNIT COSTS ARE
         RECALCULATED JUST IN CASE THE _wipval_xfer FIELDS CHANGE */

      {gprun.i ""remaxfer.p""
         "(input wr_lbr_ll_act, input wr_lbr_ll_totx,
           input-output lbr_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_bdn_ll_act, input wr_bdn_ll_totx,
           input-output bdn_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_sub_ll_act, input wr_sub_ll_totx,
           input-output sub_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_mtl_ll_act, input wr_mtl_ll_totx,
           input-output mtl_ll_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_lbr_act, input wr_lbr_totx,
           input-output lbr_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_bdn_act, input wr_bdn_totx,
           input-output bdn_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_sub_act, input wr_sub_totx,
           input-output sub_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      {gprun.i ""remaxfer.p""
         "(input wr_mtl_act, input wr_mtl_totx,
           input-output mtl_tl_wipval_xfer)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   end. /*IF WIP_QTY <> 0 */
   else
      assign
         lbr_ll_wipval_xfer = wr_lbr_ll_act - wr_lbr_ll_totx
         bdn_ll_wipval_xfer = wr_bdn_ll_act - wr_bdn_ll_totx
         sub_ll_wipval_xfer = wr_sub_ll_act - wr_sub_ll_totx
         mtl_ll_wipval_xfer = wr_mtl_ll_act - wr_mtl_ll_totx

         lbr_tl_wipval_xfer = wr_lbr_act - wr_lbr_totx
         bdn_tl_wipval_xfer = wr_bdn_act - wr_bdn_totx
         sub_tl_wipval_xfer = wr_sub_act - wr_sub_totx
         mtl_tl_wipval_xfer = wr_mtl_act - wr_mtl_totx

         unit_lbr_ll = lbr_ll_wipval_xfer / qty_chg
         unit_bdn_ll = bdn_ll_wipval_xfer / qty_chg
         unit_sub_ll = sub_ll_wipval_xfer / qty_chg
         unit_mtl_ll = mtl_ll_wipval_xfer / qty_chg

         unit_lbr_tl = lbr_tl_wipval_xfer / qty_chg
         unit_bdn_tl = bdn_tl_wipval_xfer / qty_chg
         unit_sub_tl = sub_tl_wipval_xfer / qty_chg
         unit_mtl_tl = mtl_tl_wipval_xfer / qty_chg.
end. /* IF QTY_CHG > 0 */
else do:

   /* IN CASE OF NEGATIVE RECEIPTS DETERMINE THE UNIT COST OF THE    */
   /* PREVIOUS RECEIPT BY DIVIDING THE PREVIOUSLY POSTED TOTAL COSTS */
   /* (_totx) BY THE ACCUMULATED QUANTITY PREVIOUSLY RECEIVED        */
   /* (wr_qty_cummove at the last operation). THE PREVIOUS UNIT COST */
   /* IS MULTIPLIED BY THE QUANTITY REPORTED IN THIS TRANSACTION TO  */
   /* DETERMINE THE RESULTING INCREMENTAL CHANGE TO THE _totx fields.*/

   if wr_qty_cummove <> 0 then
      assign
         unit_lbr_ll = wr_lbr_ll_totx / wr_qty_cummove
         unit_bdn_ll = wr_bdn_ll_totx / wr_qty_cummove
         unit_sub_ll = wr_sub_ll_totx / wr_qty_cummove
         unit_mtl_ll = wr_mtl_ll_totx / wr_qty_cummove

         unit_lbr_tl = wr_lbr_totx / wr_qty_cummove
         unit_bdn_tl = wr_bdn_totx / wr_qty_cummove
         unit_sub_tl = wr_sub_totx / wr_qty_cummove
         unit_mtl_tl = wr_mtl_totx / wr_qty_cummove

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.

   else

      /* REPORTING NEGATIVE PRODUCTION TAKES INVENTORY INTO WIP */
      /* THE REPORTED NEGATIVE PRODUCTION SHOULD BE TAKEN BACK  */
      /* INTO WIP AT THE LATEST GL COST                         */

      assign
         unit_lbr_ll = 0
         unit_bdn_ll = 0
         unit_sub_ll = 0
         unit_mtl_ll = sct_mtl_tl + sct_mtl_ll

         unit_lbr_tl = sct_lbr_tl + sct_lbr_ll
         unit_bdn_tl = sct_bdn_tl + sct_bdn_ll
         unit_sub_tl = sct_sub_tl + sct_sub_ll
         unit_mtl_tl = 0

         lbr_ll_wipval_xfer = unit_lbr_ll * qty_chg
         bdn_ll_wipval_xfer = unit_bdn_ll * qty_chg
         sub_ll_wipval_xfer = unit_sub_ll * qty_chg
         mtl_ll_wipval_xfer = unit_mtl_ll * qty_chg

         lbr_tl_wipval_xfer = unit_lbr_tl * qty_chg
         bdn_tl_wipval_xfer = unit_bdn_tl * qty_chg
         sub_tl_wipval_xfer = unit_sub_tl * qty_chg
         mtl_tl_wipval_xfer = unit_mtl_tl * qty_chg.

end. /*IF QTY_CHG < 0 */

assign
   wr_lbr_ll_totx = wr_lbr_ll_totx + lbr_ll_wipval_xfer
   wr_bdn_ll_totx = wr_bdn_ll_totx + bdn_ll_wipval_xfer
   wr_sub_ll_totx = wr_sub_ll_totx + sub_ll_wipval_xfer
   wr_mtl_ll_totx = wr_mtl_ll_totx + mtl_ll_wipval_xfer

   wr_lbr_totx = wr_lbr_totx + lbr_tl_wipval_xfer
   wr_bdn_totx = wr_bdn_totx + bdn_tl_wipval_xfer
   wr_sub_totx = wr_sub_totx + sub_tl_wipval_xfer
   wr_mtl_totx = wr_mtl_totx + mtl_tl_wipval_xfer

   unit_cost = unit_lbr_ll + unit_bdn_ll + unit_sub_ll + unit_mtl_ll
             + unit_lbr_tl + unit_bdn_tl + unit_sub_tl + unit_mtl_tl.

/* UPDATE COSTS */
if qty_chg <> 0 then do:

   /* THIS SECTION OF CODE WILL CREATE ANY NECESSARY CST-ADJ RECORDS  */
   assign
      unit_ovh_ll = 0
      unit_ovh_tl = 0.
   /*DO NOT INCLUDE OVERHEAD IN AVERAGING */

   for each spt_det
      fields (spt_cst_tl spt_element spt_part spt_pct_ll spt_sim spt_site)
      where spt_site = sct_site and
            spt_sim  = sct_sim  and
            spt_part = sct_part and
         truncate(spt_pct_ll,0) = 4 no-lock:
      unit_ovh_tl = unit_ovh_tl + spt_cst_tl.
   end.

   assign
      newcst = unit_mtl_tl + unit_lbr_tl + unit_bdn_tl + unit_ovh_tl
             + unit_sub_tl + unit_mtl_ll + unit_lbr_ll + unit_bdn_ll
             + unit_sub_ll + unit_ovh_ll
      reavg_yn = yes
      /* THE VALUE OF NEWCST IS ASSIGNED TO UNIT COST SINCE IT ALSO */
      /* INCLUDES OVERHEAD.                                         */
      unit_cost = newcst.

   if  (sct_mtl_tl = 0)
      and (sct_mtl_ll = unit_mtl_tl + unit_mtl_ll )
      and (sct_lbr_tl = unit_lbr_tl + unit_lbr_ll)
      and (sct_bdn_tl = unit_bdn_tl + unit_bdn_ll)
      and (sct_ovh_tl = unit_ovh_tl)
      and (sct_sub_tl = unit_sub_tl + unit_sub_ll)
      and (sct_cst_tot = newcst)
   then
      reavg_yn = no.
   else do:
      assign
         msgref  = 0
         glxflag = yes.

      /*CREATE DISCREPANCY TRANSACTION IF NECESSARY*/

      if (qty_oh) < 0
      then
         msgref = 5203.  /*beginning balance < 0*/
      else
      if (qty_oh + qty_chg <= 0)
      then
         msgref = 5202.  /*ending balance <= 0*/
      else
      if (((qty_oh * sct_cst_tot) + (qty_chg * newcst))
         / (qty_oh + qty_chg) <= 0)
      then
         msgref = 5201.     /*cost re-average <= 0*/

      if msgref <> 0 then do:
         assign
            reavg_yn = no
            oldcst   = sct_cst_tot.

         /* IF THE QOH IS NEGATIVE OR THE QUANTITY PROCESSED AT THIS OPERATION MAKES */
         /* THE QUANTITY ON HAND GO ZERO OR NEGATIVE OR THE AVERAGE COST BECOMES ZERO*/
         /* OR NEGATIVE THEN ASSIGN THE sct_det COST ELEMENTS AS SHOWN BELOW.        */

         assign
            sct_mtl_tl = 0
            sct_lbr_tl = max(0, unit_lbr_tl + unit_lbr_ll)
            sct_bdn_tl = max(0, unit_bdn_tl + unit_bdn_ll)
            sct_sub_tl = max(0, unit_sub_tl + unit_sub_ll)

            /* WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST */
            sct_ovh_tl = sct_ovh_tl
            sct_mtl_ll = max(0, unit_mtl_tl + unit_mtl_ll)
            sct_lbr_ll = 0
            sct_bdn_ll = 0
            sct_sub_ll = 0
            sct_ovh_ll = 0
            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                        + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                        + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                        + sct_sub_ll
            sct_cst_date = today
            gl_amt =  (sct_cst_tot - oldcst) * qty_oh.

         /* UPDATE TRANSACTION HISTORY RECORD */
         /* iccstinv.p IS NOW USED TO UPDATE THE TRANSACTION HISTORY RECORD */
         /* INSTEAD OF recrtr.p BECAUSE THE FORMER MAKES ONE ADDITIONAL     */
         /* SEARCH ON THE pld_det TABLE, NAMELY A SEARCH USING PARAMETERS   */
         /* PRODUCT LINE + SITE + LOCATION, WHERE THE LOCATION IS NOT BLANK */

         for first pt_mstr
            fields (pt_abc pt_avg_int pt_cyc_int pt_part
                    pt_prod_line pt_site pt_um)
            where pt_part = wo_part
         no-lock:
         end. /* FOR FIRST tr_hist */

         assign
            trmsg     = msgref
            l_ord_nbr = "CSAVG02" + wo_nbr.

         {gprun.i ""iccstinv.p""
            "(input oldcst,
              buffer sct_det,
              input l_ord_nbr,
              input wo_site,
              input icc_cogs,
              input icc_gl_sum,
              input icc_gl_tran,
              input icc_mirror,
              input icc_jrnl,
              input rndmthd,
              input pt_part,
              input pt_prod_line,
              input pt_site,
              input pt_um)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      end. /*if msgref <> 0*/
   end. /*else do (if sct_???_?l <> new???_?l)*/

   /* CREATE CST-ADJ TRANSACTION IF NECESSARY */
   /* THIS IS NECESSARY FOR THE INVENTORY     */
   /* VALUATION REPORTS AS THE AS OF EFF DT   */

   for first tr_hist
      fields (tr_effdate tr_part tr_site tr_type)
      where tr_type = "CST-ADJ"
        and tr_effdate = today
        and tr_site = wo_site
        and tr_part = wo_part no-lock:
   end. /* FOR FIRST TR_HIST */

   if not available tr_hist then do:

      /* Shiptype changed to "M" as location is ""  */

      /* UPDATE TRANSACTION HISTORY RECORD */
      {gprun.i ""recrtr.p""
         "(input recid(wo_mstr),
           input 0,
           input 0,
           input qty_chg,
           input 0)"
         }
/*GUI*/ if global-beam-me-up then undo, leave.


   end. /*if not available tr_hist*/

   if reavg_yn then do:
      /* GET G/L COST SET FOR UPDATE */
      {gpsct01.i &set=glx_set &part=wo_part &site=wo_site}

      /* UPDATE G/L AVERAGE COST */
      assign
         newmtl_ll = (sct_mtl_ll * qty_oh + mtl_tl_wipval_xfer
                   + mtl_ll_wipval_xfer) / new_qty_oh

         newmtl_tl = (sct_mtl_tl * qty_oh) / new_qty_oh
         newlbr_tl = ((sct_lbr_tl + sct_lbr_ll) * qty_oh
                   + lbr_tl_wipval_xfer + lbr_ll_wipval_xfer) / new_qty_oh
         newbdn_tl = ((sct_bdn_tl + sct_bdn_ll) * qty_oh
                   + bdn_tl_wipval_xfer + bdn_ll_wipval_xfer) / new_qty_oh
         newsub_tl = ((sct_sub_tl + sct_sub_ll) * qty_oh
                   + sub_tl_wipval_xfer + sub_ll_wipval_xfer) / new_qty_oh

         sct_mtl_ll = newmtl_ll
         sct_lbr_ll = 0
         sct_bdn_ll = 0
         sct_sub_ll = 0
         sct_ovh_ll = 0
         sct_mtl_tl = newmtl_tl
         sct_lbr_tl = newlbr_tl
         sct_bdn_tl = newbdn_tl
         sct_sub_tl = newsub_tl
         /* WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST */
         sct_ovh_tl = sct_ovh_tl

         sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                     + sct_ovh_tl + sct_sub_tl
                     + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll
                     + sct_ovh_ll + sct_sub_ll
         sct_cst_date = today.

      /* UPDATE SPT_DET */
      {gprun.i ""reupdspt.p"" "(input recid(sct_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* reavag_yn = yes */

   if cur_mthd = "avg" then do:
      /*GET CURRENT COST SET*/
      {gpsct01.i &set=cur_set &part=wo_part &site=wo_site}

      assign
         unit_ovh_ll = 0
         unit_ovh_tl = 0.
      /*DO NOT INCLUDE OVERHEAD IN AVERAGING */

      for each spt_det
         fields (spt_cst_tl spt_element spt_part spt_pct_ll spt_sim spt_site)
            where spt_site = sct_site
              and spt_sim  = sct_sim
              and spt_part = sct_part and
            truncate(spt_pct_ll,0) = 4 no-lock:
         unit_ovh_tl = unit_ovh_tl + spt_cst_tl.
      end.

      assign
         newcst = unit_mtl_tl + unit_lbr_tl + unit_bdn_tl + unit_ovh_tl
                + unit_sub_tl + unit_mtl_ll + unit_lbr_ll + unit_bdn_ll
                + unit_sub_ll + unit_ovh_ll
         reavg_yn = yes.

      if  (sct_mtl_tl = 0)
         and (sct_mtl_ll = unit_mtl_tl + unit_mtl_ll)
         and (sct_lbr_tl = unit_lbr_tl + unit_lbr_ll)
         and (sct_bdn_tl = unit_bdn_tl + unit_bdn_ll)
         and (sct_ovh_tl = unit_ovh_tl)
         and (sct_sub_tl = unit_sub_tl + unit_sub_ll)
         and (sct_cst_tot = newcst)
      then
         reavg_yn = no.
      else do:
         assign
            reavg_yn = yes
            curflag = yes.
         /* DETERMINE IF COST SHOULD BE AVERAGED OR REPLACED */
         if (qty_oh + qty_chg <= 0)
            or (((qty_oh * sct_cst_tot) + (qty_chg * newcst))
            / (qty_oh + qty_chg) <= 0)
            or (qty_oh) <= 0
         then
            use_last = yes.

      end. /* Need to Reaverage */

      if reavg_yn and not use_last then do:

         /* UPDATE AVERAGE CURRENT COST */
         assign
            sct_mtl_ll = newmtl_ll
            sct_lbr_ll = 0
            sct_bdn_ll = 0
            sct_sub_ll = 0
            sct_ovh_ll = 0
            sct_mtl_tl = newmtl_tl
            sct_lbr_tl = newlbr_tl
            sct_bdn_tl = newbdn_tl
            sct_sub_tl = newsub_tl
            /* WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST */
            sct_ovh_tl = sct_ovh_tl

            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                        + sct_ovh_tl + sct_sub_tl
                        + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll
                        + sct_ovh_ll + sct_sub_ll
            sct_cst_date = today.

         /*UPDATE SPT_DET*/
         {gprun.i ""reupdspt.p"" "(input recid(sct_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.

      end. /* reavg_yn & not use_last */

   end. /* cur_mthd = "avg" */

   if cur_mthd = "last" or use_last then do:
      curflag = yes.
      /* GET CURRENT COST SET */
      {gpsct01.i &set=cur_set &part=wo_part &site=wo_site}

      /* UPDATE LAST CURRENT COST */
      assign
         sct_mtl_tl = 0
         sct_lbr_tl = max(0,unit_lbr_tl + unit_lbr_ll)
         sct_bdn_tl = max(0,unit_bdn_tl + unit_bdn_ll)
         sct_sub_tl = max(0,unit_sub_tl + unit_sub_ll)
         /* WE DO NOT WANT TO CHANGE THE THIS-LEVEL OVERHEAD COST    */
         sct_ovh_tl = sct_ovh_tl
         sct_mtl_ll = max(0,unit_mtl_tl + unit_mtl_ll)
         sct_lbr_ll = 0
         sct_bdn_ll = 0
         sct_sub_ll = 0
         sct_ovh_ll = 0

         sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                     + sct_ovh_tl + sct_sub_tl
                     + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll
                     + sct_ovh_ll + sct_sub_ll
         sct_cst_date = today.

      /* UPDATE SPT_DET */
      {gprun.i ""reupdspt.p"" "(input recid(sct_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* if cur_mthd = "last" */

   /* ADD csavg2b.p SO THAT THE COST SIMULATION TOTAL DETAIL      */
   /* MATCHES THE COST SIMULATION ITEM DETAIL                     */
   {gprun.i ""csavg2b.p""
      "(input wo_part,
        input wo_site,
        input cur_set,
        input curflag,
        input glx_set,
        input glxflag)"
      }
/*GUI*/ if global-beam-me-up then undo, leave.


end.  /* if qty_chg > 0 then do .... */
